package com.guli.ware.controller;

import java.util.Arrays;
import java.util.Map;

import com.guli.ware.dto.PurchaseDemandingMergeTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guli.ware.entity.PurchaseEntity;
import com.guli.ware.service.PurchaseService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 采购单信息
 * @author zzb
 * @email 1626131069@qq.com
 * @date 2023-11-24 22:39:12
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 采购人完成采购单
     * @param purchaseOrderId
     * @return
     */
    @GetMapping("/finished/{purchaseOrderId}")
    public R finishOrder(@PathVariable(value = "purchaseOrderId") Long purchaseOrderId, String isPurchase) {
        purchaseService.finishOrder(purchaseOrderId, isPurchase);
        return R.ok();
    }

    /**
     * 采购人领取采购单
     * @param purchaseOrderId
     * @return
     */
    @GetMapping("/receive/{purchaseOrderId}")
    public R receiveOrder(@PathVariable(value = "purchaseOrderId") Long purchaseOrderId) {
        purchaseService.receiveOrder(purchaseOrderId);
        return R.ok();
    }

    /**
     * 合并采购需求为采购单
     * @return
     */
    @PostMapping("/merge")
    public R mergePurchaseOrder(@RequestBody PurchaseDemandingMergeTo purchaseDemandingMergeTo) {
        purchaseService.mergePurchaseOrder(purchaseDemandingMergeTo);
        return R.ok();
    }

    /**
     * 查询新建或已分配的采购单
     * @return
     */
    @GetMapping("/unreceive/list")
    public R unreceiveList() {
        PageUtils pages = purchaseService.unreceiveList();
        return R.ok().put("page", pages);
    }

    /**
     * 采购单列表
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);
        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchaseService.save(purchase);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
